<template>
    <div>
        <div class="header-info">
            <p>我的资料</p>
            <div v-if="jv.main.user.isAdmin">
                <el-button size="mini" @click="pwd_dlg=true"> 修改密码</el-button>
                <el-button size="mini" @click="save_click" type="primary"> 保存</el-button>
            </div>
        </div>
        <el-row :gutter="12" type="flex" ref="card">
            <el-col>
                <el-card shadow="always">
                    <div slot="header">
                        <span>基本信息</span>
                    </div>
                    <kv label="姓名">
                        <el-input v-model="info.name" chk="*"/>
                    </kv>
                    <kv label="登录名">
                        <el-input v-model="info.loginName" chk="*"/>
                    </kv>
                    <kv label="手机号">
                        <el-input v-model="info.mobile" chk="*"/>
                    </kv>
                </el-card>
            </el-col>
        </el-row>

        <el-dialog ref="pwd_dlg" title="修改密码" :visible.sync="pwd_dlg" :close-on-click-modal="false" :show-close="false"
                   width="50%">
            <change_pwd @change="pwd_dlg=false"></change_pwd>
        </el-dialog>
    </div>
</template>
<style scoped>
    >>> .kv .k {
        flex: 4 !important;
        min-width: auto !important;
    }
</style>
<script>
    /**
     * Created by CodeGenerator
     */
    import change_pwd from "../site_com/change_pwd";

    export default {
        components: {change_pwd},
        data() {
            return {
                pwd_dlg: false,
                info: {}
            }
        },
        computed: {
            action() {
                return this.info.id ? "edit" : "add";
            },
            action_name() {
                return {add: "添加", edit: "修改"}[this.action]
            }
        },
        mounted() {
            this.info.id = jv.main.user.id;
            this.loadData();
        },
        methods: {
            loadData() {
                this.$http.post("/site/user/detail/" + this.info.id).then(res => {
                    this.info = res.data.data;
                });
            },
            save_click() {
                //校验
                if (this.$refs.card.chk() == false) {
                    return;
                }

                this.$http.post("/site/user/save", this.info).then(res => {
                    jv.info(this.action_name + " 成功");
                    if (this.action == "add") {
                        this.$router.push("/site/user/edit/" + res.data.data)
                    } else if (this.action == "edit") {
                        this.$router.push("/site/user/list")
                    }
                })
            }
        }
    }
</script>
